#include "stdio.h"
#include "string.h"

struct typett
{
	int num;
	char name[100];
	int tot;
	int wine[30000];
	int amount[30000];
	int time;
	long int price;

}list[10000];

struct typetq
{
	char name[100];
	int totamount;
	int price;
}jiu[30000];

int n_list,n_jiu,time[1000],num[1000];


int check(int k)
{
	int i;
	for (i=1;i<=list[k].tot;i++)
	{
		if  (jiu[ list[k].wine[i] ].totamount - list[k].amount[i] < 0 ) return 0;
	}
	return 1;
}

void dec(int k)
{
	int i;
	for (i=1;i<=list[k].tot;i++)
	{
		 jiu[ list[k].wine[i] ].totamount -= list[k].amount[i] ;
	}
	
}

int getsite(char s[1000])
{
	int i,flag;
	flag=0;
	for (i=1;i<=n_jiu;i++)
	{
		
		if (strcmp(jiu[i].name,s)==0)
		{
			flag=i;
			break;
		}
	}
	if (flag) return flag;
	else
	{
		n_jiu++;
		strcpy(jiu[n_jiu].name,s);
		jiu[n_jiu].price=0;
		jiu[n_jiu].totamount=0;
		return n_jiu;
	}

	
}

int getwine(char s[1000])
{
	int i,flag;
	flag=0;
	for (i=1;i<=n_jiu;i++)
	{
		if (strcmp(jiu[i].name,s)==0)
		{
			flag=i;
			break;
		}
	}
	return flag;
	
}

int getlist(char s[1000])
{
	int i,flag;
	flag=0;
	for (i=1;i<=n_list;i++)
	{
		if (strcmp(list[i].name,s)==0)
		{
			flag=i;
			break;
		}
	}
	return flag;
	
}


void main()
{
	int icase,ncase,i,j,k,n,a,b,last;
	long int tt;
	char s[1000];


	scanf("%d",&ncase);
	for (icase=1;icase<=ncase;icase++)
	{
		n_list=0;
		n_jiu=0;
		last=0;
		scanf("%d",&n_list);
		for (i=1;i<=n_list;i++)
		{
			list[i].num=i;
			list[i].price=0;
			list[i].time=0;
			scanf("%s%d",list[i].name,&list[i].tot);
			for (j=1;j<=list[i].tot;j++)
			{
				scanf("%s%d",s,&list[i].amount[j]);
				k=getsite(s);
				list[i].wine[j]=k;
			}
		}

		scanf("%d",&n);
		for (i=1;i<=n;i++)
		{
			scanf("%s%d%d",s,&a,&b);
			k=getwine(s);
			if (k)
			{
				jiu[k].totamount=a;
				jiu[k].price=b;
			}
		}
		/* output test 
		for (i=1;i<=n_list;i++)
		{
			printf("%s \n",list[i].name);
			for (j=1;j<=list[i].tot;j++)
			{
				printf("%d  %d \n",list[i].wine[j],list[i].amount[j]);
			}
			printf("\n");
		}
		for (i=1;i<=n_jiu;i++) printf("No. %d :%s %d\n",i,jiu[i].name,jiu[i].totamount); */

/*		for (i=1;i<=n_list;i++)
		{
			tt=0;
			for (j=1;j<=list[i].tot;j++)
			{
				tt+=jiu[ list[i].wine[j] ].price * list[i].amount[j];
			}
			list[i].price=tt*3+10000;
		}
*/
		scanf("%d",&n);
		last=0;
		for (i=1;i<=n;i++)
		{
			scanf("%s",s);
			k=getlist(s);
			b=check(k);
			a=k;
			
			while ((!b)||(last==a))
			{
				a++;	
				if (a>n_list) a=1;
				if (a==k) break;
				b=check(a);
			}
			if ((b)&&(last!=a))
			{
				list[a].time++;
				dec(a);
				last=a;
			}

		}

		printf("Scenario %d top cocktails:\n",icase);
		for (i=1;i<=n_list;i++) 
		{
			time[i]=list[i].time;
			num[i]=i;
		}
		
		for (i=1;i<n_list;i++)
			for (j=i+1;j<=n_list;j++)
			{
				if ((time[i]<time[j])||
					(time[i]==time[j]&&num[i]>num[j]))
				{
					time[0]=time[i];
					time[i]=time[j];
					time[j]=time[0];
					num[0]=num[i];
					num[i]=num[j];
					num[j]=num[0];
				}
			}

		for (i=1;i<=n_list;i++)
		{
			if (time[i]==0||i>10) break;
			tt=0;
			for (j=1;j<=list[num[i]].tot;j++)
			{
				tt+=jiu[ list[num[i]].wine[j] ].price * list[num[i]].amount[j];
			}
		    tt=tt*3+10000;
			printf("%d %s %d %ld\n",i,list[num[i]].name,list[num[i]].time,tt);
			
		}

	}
	
}

